package com.zzj.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzj.bean.UserRole;
import com.zzj.service.UserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Map;

/**
 * <p>
 * InnoDB free: 8192 kB 前端控制器
 * </p>
 *
 * @author 花
 * @since 2022-03-04
 */
@RestController
@RequestMapping("/user-role")
@Api
@CrossOrigin
public class UserRoleController {
    @Autowired
    private UserRoleService service;

    @PostMapping("add")
    @ApiOperation("给用户添加角色：赋权")
    public boolean addRole(@RequestBody Map<String,Object> map){
        String userId = (String) map.get("userId");
        ArrayList<String> roleId = (ArrayList<String>) map.get("roleId");
        roleId.forEach(id->{
            UserRole userRole = new UserRole();
            userRole.setUid(userId);
            userRole.setRid(id);
            service.save(userRole);
        });
        return true;
    }

    @PostMapping("del")
    @ApiOperation("删除用户一些角色：赋权")
    public boolean delRole(@RequestBody Map<String,Object> map){
        String userId = (String) map.get("userId");
        ArrayList<String> roleId = (ArrayList<String>) map.get("roleId");
        QueryWrapper<UserRole> qw = new QueryWrapper<>();
        qw.eq("uid",userId);
        qw.in("rid",roleId);
        return service.remove(qw);
    }

}

